<template>
  <div class="wrapper">
    <div class="top">
      <div class="top__header">
        <div
          class="iconfont top__header__back icon-shangyiyehoutuifanhui"
          @click="()=>{handleBackClick()}"
        ></div>
        确认订单
      </div>
      <div class="top__receiver">
        <div class="top__receiver__title">收货地址</div>
        <div v-if="address" @click="handleAddressClick">
          <div class="top__receiver__address">
            {{ address.city }}{{ address.department }}{{ address.houseNumber }}
          </div>
          <div class="top__receiver__info">
            <span class="top__receiver__info__name">{{address.name}}</span>
            <span class="top__receiver__info__name">{{address.phone}}</span>
          </div>
          <div
            class="
              iconfont
              top__receiver__icon
              icon-xiayiyeqianjinchakangengduo
            "
          ></div>
        </div>
        <div v-else>
          <div class="top__receiver__address">暂无可用地址</div>
        </div>
      </div>
    </div>
    <div class="products">
      <div class="products__title">{{shopName}}</div>
      <div class="products__list">
        <div class="products__item" v-for="item in productList" :key="item._id" v-show="item.count>0">
          <img class="products__item__img" :src="item.imgUrl" />
          <div class="products__item__detail">
            <h4 class="products__item__title">{{item.name}}</h4>
            <p class="products__item__price">
              <span class="products__item__single">
                <span class="products__item__yen">
                    &yen;{{ item.price }} x {{ item.count }}
                </span>
              </span>
              <span class="products__item__total">
                <span class="products__item__yen">
                    &yen;{{ (item.price * item.count).toFixed(2) }}
                </span>
              </span>
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="order">
        <div class="order__price">实付金额： <b>¥ {{total.sum}}</b></div>
        <div
            v-if="address"
            class="order__btn"
            @click="() => handleShowConfirmChange(true)"
        >提交订单</div>
    </div>
    <div
        class="mask"
        v-show="showConfirm"
        @click="() => handleShowConfirmChange(false)"
    >
        <div class="mask__content" @click.stop>
        <h3 class="mask__content__title">确认要离开收银台？</h3>
        <p class="mask__content__desc">请尽快完成支付，否则将被取消</p>
        <div class="mask__content__btns">
            <div
            class="mask__content__btn mask__content__btn--first"
            @click="() => handleConfirmOrder(true)"
            >取消订单</div>
            <div
            class="mask__content__btn mask__content__btn--last"
            @click="() => handleConfirmOrder(false)"
            >确认支付</div>
        </div>
        </div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'
import {ref} from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex'
import { useCommonCartEffect } from '../../effects/cartEffects';
import useCommonAddressEffect from '../../effects/addressEffect';
import {post} from '../../utils/request';

// 下单相关逻辑
const useMakeOrderEffect = (shopId,productList,shopName,address)=>{
  const store = useStore();
  const router = useRouter();
  const handleConfirmOrder = async (isCanceled) => {
      const products = []
      for(let i in productList.value) {
        const product = productList.value[i]
        products.push({id: product._id, num: product.count})
      }
      try {
        const result = await post('/api/order', {
          addressId: address.value._id,
          shopId,
          shopName: shopName.value,
          isCanceled,
          products
        })
        if (result?.errno === 0) {
          store.commit('clearCartData', shopId)
          router.push({ name: 'OrderList' })
        } 
      } catch (e) {
        // 提示下单失败
        console.log(e);
      }
  }
  return {handleConfirmOrder}
}
// 蒙层展示相关逻辑
const useShowMaskEffect = ()=>{
  const showConfirm = ref(false);
   const handleShowConfirmChange = (status)=>{
      showConfirm.value = status;
  }
  return {showConfirm,handleShowConfirmChange}
}

const useAddressEffect = () => {
  const route = useRoute();
  const addressId = route.params.addressId;
  const currentAddress = computed(() => {
    const store = useStore();
    const addressList = store.state.addressList;
    return addressList.find(item => item._id === addressId) || addressList[0]
  })
  return currentAddress
}

export default {
  name: "OrderConfirmation",
  setup() {
    const router = useRouter();
    const route = useRoute();
    const shopId = parseInt(route.params.id,10);

    const address = useAddressEffect();
    
    const handleBackClick = ()=>{
      router.back();
    }
    const { productList,shopName,total } = useCommonCartEffect(shopId);
    const { handleConfirmOrder} = useMakeOrderEffect(shopId,productList,shopName,address);
    const {showConfirm,handleShowConfirmChange} = useShowMaskEffect();

    const handleAddressClick = () => { router.push(`/chooseAddressList/${shopId}`) }

    const { getAddressList } = useCommonAddressEffect()
    getAddressList()
   
    return { 
      productList,shopName,total,showConfirm,handleBackClick,
      handleShowConfirmChange,handleConfirmOrder,handleAddressClick,address
    };
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: #f8f8f8;
  
}
.top {
  position: relative;
  height: 1.96rem;
  background-size: 100% 1.59rem;
  background-image: linear-gradient(0deg, rgba(0, 145, 255, 0) 4%, #0091ff 50%);
  background-repeat: no-repeat;
  &__header {
    position: relative;
    padding-top: 0.2rem;
    line-height: 0.24rem;
    color: #fff;
    text-align: center;
    font-size: 0.16rem;
    &__back {
      position: absolute;
      left: 0.18rem;
      font-size: 0.22rem;
    }
  }
  &__receiver {
    position: absolute;
    left: 0.18rem;
    right: 0.18rem;
    bottom: 0;
    height: 1.11rem;
    background: #fff;
    border-radius: 0.04rem;
    &__title {
      line-height: 0.22rem;
      padding: 0.16rem 0 0.14rem 0.16rem;
      font-size: 0.16rem;
      color: #333;
    }
    &__address {
      line-height: 0.2rem;
      padding: 0 0.4rem 0 0.16rem;
      font-size: 0.14rem;
      color: #333;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    &__info {
      padding: 0.06rem 0 0 0.16rem;
      &__name {
        margin-right: 0.06rem;
        line-height: 0.18rem;
        font-size: 0.12rem;
        color: #666;
      }
    }
    &__icon {
      position: absolute;
      right: 0.16rem;
      top: 0.5rem;
      color: #666;
      font-size: 0.2rem;
    }
  }
}

.products {
  margin: 0.16rem 0.18rem 0.1rem 0.18rem;
  background: #fff;
  &__title {
    padding: 0.16rem;
    font-size: 0.16rem;
    color: #333333;
  }
  &__list {
    background: #fff;
    overflow-y: scroll;
    margin: 0 0.18rem;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0.6rem;
    top: 2.6rem;
  }
  &__item {
    position: relative;
    display: flex;
    padding: 0 0.16rem 0.16rem 0.16rem;
    border-bottom: 0.01rem solid #f1f1f1;
    &__detail {
      flex: 1;
    }
    &__img {
      width: 0.46rem;
      height: 0.46rem;
      margin-right: 0.16rem;
    }
    &__title {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #333333;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    &__price {
      margin: 0.06rem 0 0 0;
      display: flex;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #e93b3b;
    }
    &__total {
        flex: 1;
        text-align: right;
        color: #000;
    }
    &__yen {
      font-size: 0.12rem;
    }
  }
  &__item:last-child {
    border-bottom: none;
  }
}

.order {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  height: .49rem;
  line-height: .49rem;
  background: #FFF;
  &__price {
    flex: 1;
    text-indent: .24rem;
    font-size: .14rem;
    color: #333;
    b{
      color: #e93b3b;
    }
  }
  &__btn {
    width: .98rem;
    background: #0091FF;
    color: #FFF;
    text-align: center;
    font-size: .14rem;
  }
}
.mask {
  z-index: 1;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: rgba(0, 0, 0, .5);
  &__content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 3rem;
    height: 1.56rem;
    background: #FFF;
    text-align: center;
    border-radius: .04rem;
    &__title {
      margin: .24rem 0 0 0;
      line-height: .26rem;
      font-size: .18rem;
      color: #333;
    }
    &__desc {
      margin: .08rem 0 0 0;
      font-size: .14rem;
      color: #666;
    }
    &__btns {
      display: flex;
      margin: .24rem .58rem;
    }
    &__btn {
      flex: 1;
      width: .8rem;
      line-height: .32rem;
      border-radius: .16rem;
      font-size: .14rem;
      &--first {
        margin-right: .12rem;
        border: .01rem solid #0091FF;
        color: #0091FF;
      }
      &--last {
        margin-left: .12rem;
        background: #0091FF;
        color: #fff;
      }
    }
  }
}
</style>
